import java.io.*;
import java.net.*;

public class Loader {

	static String generateURL(String[] symbols) {
		String acc = "";
		for (String s : symbols)
			acc += s + "+";
		return "http://finance.yahoo.com/d/quotes.csv?s="
				+ acc.substring(0, acc.length() - 1) + "&f=snl1d1t1ohgdr";
	}

	static void urlConn(String[] symbols) {
		URL u;
		InputStream is = null;
		DataInputStream dis;
		String s;
		String fileName = "C:\\Users\\V\\Desktop\\Java Practice\\TradeData\\Data.csv";
		
		try {
			FileWriter writer = new FileWriter(fileName);
		
		
			try {
				String url = generateURL(symbols);
				System.out.println(url);
				u = new URL(url);
	
				is = u.openStream();
	
				dis = new DataInputStream(new BufferedInputStream(is));
	
				while ((s = dis.readLine()) != null) {
					writer.append(s);
					writer.append('\n');
					System.out.println(s);
				}
	
			} catch (MalformedURLException mue) {
	
				System.out.println("MalformedURLException.");
				mue.printStackTrace();
				System.exit(1);
	
			} catch (IOException ioe) {
	
				System.out.println("IOException.");
				ioe.printStackTrace();
				System.exit(1);
	
			} finally {
	
				try {
					is.close();
				} catch (IOException ioe) {
				}
			}
			writer.flush();
		    writer.close();
		    
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public static void main(String[] args) {
		String[] tickers = { "GOOG", "GS" };
		urlConn(tickers);
	}
}
